package hot;

import java.util.ArrayList;
import java.util.List;

public class wusi {
//    https://leetcode.cn/problems/spiral-matrix/?envType=study-plan-v2&envId=top-100-liked
    public static List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> ret=new ArrayList<>();
        int m=matrix.length;
        int n=matrix[0].length;
        int left=0;
        int right=n-1;
        int top=0;
        int bottom=m-1;
        while(left<=right&&top<=bottom){
            for(int i=left;i<=right;i++){
                ret.add(matrix[left][i]);
            }
            top++;
            for(int i=top;i<=bottom;i++){
                ret.add(matrix[i][right]);
            }
            right--;
            if(bottom>=top){
                for(int i=right;i>=left;i--){
                    ret.add(matrix[bottom][i]);
                }
                bottom--;
            }
//            for(int i=right;i>=left;i--){
//                ret.add(matrix[bottom][i]);
//            }
//            bottom--;
//            if(left<=right)
            if(left<=right){
                for(int i=bottom;i>=top;i--){
                    ret.add(matrix[i][left]);
                }
                left++;
            }
        }
        return ret;

    }

    public static void main(String[] args) {
//        int[][] nums={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        int[][] nums={{7},{9},{6}};
        List<Integer> list = spiralOrder(nums);
        System.out.println(list);


    }
}
